﻿/**************************
 * Author: Hoangvx6b
 * Date: 02/04/2013
 * Module: Back up
 * Project: OFB
 **************************/


using System;
using System.Collections.Generic;
using OFB.BLL.Context;
using OFB.BLL.ViewModel.Backup;
using OFB.Domain;
using System.Data;

namespace OFB.BLL.Repository
{
    /// <summary>
    /// Class content all BLL of module BackUp
    /// </summary>
    public class BackupBLL : IBackupBLL
    {
        private readonly BackupContext context;

        /// <summary>
        /// Contructor
        /// </summary>
        public BackupBLL()
        {
            context = new BackupContext();
        }

        /// <summary>
        /// Load all year that have at least one course
        /// </summary>
        /// <param name="isBackuped"></param>
        /// <returns></returns>
        public ICollection<BackupYear> GetBackupYear(bool isBackuped = false)
        {
            return context.BuCreate.Init("Backup_GetYear",
                                        context.BuCreate.BuildParameter("IsBackuped",DbType.Boolean, isBackuped))
                .MapCollection<BackupYear>()
                .GetCollection();
        }

        /// <summary>
        /// Get all course can back up on that year
        /// </summary>
        /// <param name="year"></param>
        /// <param name="isBackuped"></param>
        /// <returns></returns>
        public ICollection<Course> GetCourses(int year, bool isBackuped = false)
        {
            return context.Course.Init("Backup_GetCourses", 
                                        context.Course.BuildParameter("year", DbType.Int32, year),
                                        context.Course.BuildParameter("IsBackuped",DbType.Boolean, isBackuped))
                .MapCollection<Course>()
                .GetCollection();
        }

        /// <summary>
        /// Get all course already backup in database
        /// </summary>
        /// <param name="year"></param>
        /// <param name="isBackuped"></param>
        /// <returns></returns>
        public ICollection<BackUpTable> GetCoursesBackuped(int year, bool isBackuped = true)
        {
            return context.BuLoad.Init("Backup_GetCourses", 
                                       context.Course.BuildParameter("year", DbType.Int32, year),
                                       context.Course.BuildParameter("IsBackuped", DbType.Boolean, isBackuped))
               .MapCollection<BackUpTable>()
               .GetCollection();
        }

        /// <summary>
        /// Get all subject in a course
        /// </summary>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public ICollection<Subject> GetSubjects(int courseId)
        {
            return context.Subject.Init("Backup_GetSubject", 
                                        context.Subject.BuildParameter("CourseId",DbType.Int32, courseId))
                .MapCollection<Subject>()
                .GetCollection();
        }

        /// <summary>
        /// Get trainer in course
        /// </summary>
        /// <param name="courseId"></param>
        /// <param name="subjectId"></param>
        /// <returns></returns>
        public ICollection<BackupTrainer> GetTrainers(int courseId, int? subjectId = null)
        {
            return context.BuTrainer.Init("Backup_GetTrainer", 
                                        context.BuTrainer.BuildParameter("CourseId",DbType.Int32, courseId),
                                        context.BuTrainer.BuildParameter("SubjectId", DbType.Int32, subjectId))
                .MapCollection<BackupTrainer>()
                .GetCollection();
        }

        /// <summary>
        /// Get topics in a subject
        /// </summary>
        /// <param name="courseId"></param>
        /// <param name="subjectId"></param>
        /// <param name="trainerId"></param>
        /// <returns></returns>
        public ICollection<BackupTopic> GetTopics(int courseId, int subjectId, string trainerId = null)
        {
            return context.BuTopic.Init("Backup_GetTopic", 
                                        context.BuTopic.BuildParameter("CourseId", DbType.Int32, courseId),
                                        context.BuTopic.BuildParameter("SubjectId", DbType.Int32, subjectId),
                                        context.BuTopic.BuildParameter("TrainerId", DbType.Int32, trainerId))
                .MapCollection<BackupTopic>()
                .GetCollection();
        }

        /// <summary>
        /// Load backup view
        /// </summary>
        /// <param name="courseId"></param>
        /// <param name="subjectId"></param>
        /// <param name="trainerId"></param>
        /// <param name="topicId"></param>
        /// <param name="fromDate"></param>
        /// <param name="toDate"></param>
        /// <returns></returns>
        public ICollection<BackupView> GetBackupView(int courseId, int subjectId, string trainerId = null, int? topicId = null, DateTime? fromDate = null, DateTime? toDate = null)
        {
            return
                context.BuView.Init("BackUp_Search", 
                                        context.BuLoad.BuildParameter("CourseId", DbType.Int32, courseId),
                                        context.BuLoad.BuildParameter("SubjectId", DbType.Int32, subjectId),
                                        context.BuLoad.BuildParameter("TrainerId", DbType.Single, trainerId),
                                        context.BuLoad.BuildParameter("TopicId", DbType.Int32, topicId),
                                        context.BuLoad.BuildParameter("FromDate", DbType.DateTime, fromDate),
                                        context.BuLoad.BuildParameter("FinishDate", DbType.DateTime, toDate))
                    .MapCollection<BackupView>()
                    .GetCollection();
        }

        /// <summary>
        /// Create a new backup
        /// </summary>
        /// <param name="year"></param>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public int CreateBackup(string year, int courseId)
        {
            IDataParameter returnvalue = context.BuCreate.BuildParameter("ReturnId", DbType.Int32,
                                                                       ParameterDirection.Output);
            context.BuCreate.Execute("BackUp_Create",context.BuCreate.BuildParameter("Year",DbType.String,year),
                                                                                   context.BuCreate.BuildParameter("CourseId",DbType.Int32, courseId),
                                                                                    returnvalue);
            return Convert.ToInt32(returnvalue.Value);
        }
    }
}
